package com.ea.eadp.nexus.test.pageDefinition;

import org.openqa.selenium.*;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.common.TestUtil;

/**
 * User: woodywu
 */
public class Signin_Web extends BasePage {
    public static final String DEFAULTURI = baseUri + TestUtil.SigninWebConnectURI();
    public static final String cancelButton = "btnCancel";

    public Signin_Web(WebDriver driver) {
        super(driver, Signin_Web.class);
        this.setUri(DEFAULTURI);
    }

    public void navigateToLockbox() {
        driver.switchTo().window(this.operationWindowHandle);
        String lockUri = TestUtil.getLockboxUrl();
        log.info(lockUri);
        driver.navigate().to(lockUri);
    }

    public void setSigninEmail(String email) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(email);
        PageOperation.SendKeyToElementAfterWait(driver, "email", email);
    }

    public void setUpgradeOriginId(String originId) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(originId);
        PageOperation.SendKeyToElementAfterWait(driver, "originId", originId);
    }

    public void setSigninPassword(String password) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(password);
        PageOperation.SendKeyToElementAfterWait(driver, "password", password);
    }

    public void checkGoogleCaptcha(boolean check) {
        driver.switchTo().window(this.operationWindowHandle);
        String xpathOfIFrame = "//li[@id='captcha-container2']/div/div/div/iframe";
        driver.switchTo().frame(driver.findElement(By.xpath(xpathOfIFrame)));
        String xpath = "//span[@id='recaptcha-anchor']/div[5]";
        if (check && !driver.findElement(By.xpath(xpath)).isSelected()) {
            driver.findElement(By.xpath(xpath)).click();
        } else if (!check && driver.findElement(By.xpath(xpath)).isSelected()) {
            driver.findElement(By.xpath(xpath)).click();
        }
    }
    public void checkRememberMe(boolean check) {
        driver.switchTo().window(this.operationWindowHandle);
        WebElement checkbox = driver.findElement(By.id("rememberMe"));
        WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", checkbox);
        if (check && !checkbox.isSelected()) {
            parent.click();
        } else if (!check && checkbox.isSelected()) {
            parent.click();
        }
    }

    public void setSigninSQA(String answer) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(answer);
        PageOperation.SendKeyToElementAfterWait(driver, "securityAnswer", answer);
    }
    public void setResetPasswordEmail(String email) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        if (!TestUtil.getCurrentUrl(driver).contains("/resetPassword")) {
            throw new Exception("setResetPasswordEmail must in page /resetPassword");
        }
        log.info(email);
        PageOperation.SendKeyToElementAfterWait(driver, "email", email);
    }

    public void setResetPasswordCapthca(String capthca) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        if (!TestUtil.getCurrentUrl(driver).contains("/resetPassword")) {
            throw new Exception("setResetPasswordEmail must in page /resetPassword");
        }
        log.info(capthca);
        PageOperation.SendKeyToElementAfterWait(driver, "recaptcha_response_field", capthca);
    }

    public void clickToSigninComplete() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath("//div[@id='panel-login']/div/div[2]/a/span/span")).click();
        Thread.sleep(3000);
        if (!TestUtil.getCurrentUrl(driver).contains("?code=")
                && !TestUtil.getCurrentUrl(driver).contains("&code=")
                && !TestUtil.getCurrentUrl(driver).contains("s2")
                && !TestUtil.getCurrentUrl(driver).contains("execution")
                && !TestUtil.getCurrentUrl(driver).contains("aboutme")) {
            throw new Exception("not login success");
        }
    }

	public void clickToSigninCompleteLockBox() throws Exception {
		driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath("//div[@id='panel-login']/div/div[2]/a/span/span")).click();
		Thread.sleep(3000);
		if (!TestUtil.getCurrentUrl(driver).contains("?code=")&& !TestUtil.getCurrentUrl(driver).contains("s2") && !TestUtil.getCurrentUrl(driver).contains("execution") &&
				!TestUtil.getCurrentUrl(driver).contains("aboutme")) {
			throw new Exception("not login success");
		}
	}

    public void clickToSigninError() {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@id='panel-login']/div/div[2]/a/span/span")).click();
        PageOperation.WebDriverWaitByXpath(driver, "//form[@id='loginForm']/div/span");
    }

    public String getError() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//form[@id='loginForm']/div[1]/div/div")).getText();
    }

    public void clickToResetPassword() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//li[@class='password-container']/a[@class='forgot-password-link']")).click();
        PageOperation.WebDriverWaitByXpath(driver, "//div[@id='link_no_error']/h1");
        if (!TestUtil.getCurrentUrl(driver).contains("/resetPassword?")) {
            throw new Exception("validate goes to 'reset password' page.");
        }
    }

    public void clickToSubmitResetPassword() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@class='panel-action-area']/a/span/span")).click();
        PageOperation.WebDriverWaitByXpath(driver, "//div[@id='panel-reset-password-request-confirmation']");
    }

    public void clickToSubmitResetPassword(String path, String regex) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@class='panel-action-area']/a/span/span")).click();
        PageOperation.WebDriverWaitByXpath(driver, path);
        String content = driver.findElement(By.xpath(path)).getText();
        if (!content.matches(getContainsRegex(regex))) {
            throw new Exception("expected [" + regex + "] but found [" + content + "]");
        }
    }

    public void clickToSubmitSQA() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        By btn_submit = By.id("btnSQASubmit");
        driver.findElement(btn_submit).click();
       }

    public void clickToSigninComplete(String path, String regex) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath("//div[@id='panel-login']/div/div[2]/a/span/span")).click();
        PageOperation.WebDriverWaitByXpath(driver, path);
        String content = driver.findElement(By.xpath(path)).getText();
        if (!content.matches(getContainsRegex(regex))) {
            throw new Exception("expected [" + regex + "] but found [" + content + "]");
        }        
    }

	public void clickToSigninCompleteLockBox(String path,String regex)throws Exception{
		driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath(".//*[@id='btnLogin']/span/span")).click();
		PageOperation.WebDriverWaitByXpath(driver, path);
		String content = driver.findElement(By.xpath(path)).getText();
		if (!content.matches(getContainsRegex(regex))) {
			throw new Exception("expected [" + regex + "] but found [" + content + "]");
		}
	}
	public void clickToSigninCompleteMobile(String path,String regex)throws Exception{
		driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath(".//*[@id='loginBtn']/input[2]")).click();
		PageOperation.WebDriverWaitByXpath(driver, path);
		String content = driver.findElement(By.xpath(path)).getText();
		if (!content.matches(getContainsRegex(regex))) {
			throw new Exception("expected [" + regex + "] but found [" + content + "]");
		}
	}

    public void clickToUpgradeComplete(String path, String regex) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@class='panel-action-area']/a[1]/span/span")).click();
        PageOperation.WebDriverWaitByXpath(driver, path);
        String content = driver.findElement(By.xpath(path)).getText();
        if (!content.matches(getContainsRegex(regex))) {
            throw new Exception("expected [" + regex + "] but found [" + content + "]");
        }
    }
	public void clicktoIncompleteAccount()throws Exception{
		driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath("//div[@class='panel-action-area']/a[1]/span/span")).click();
	}


	public void clickToSigninEmailComplete()throws Exception{
		driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath("//div[@class='panel-action-area']/a[1]/span/span")).click();
	}
	public void clickToSigninEmailCompleteLockBox()throws Exception{
		driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath("//div[@class='panel-action-area']/a[1]/span/span")).click();
	}

    public void clickToUpgradeComplete() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@class='panel-action-area']/a[1]/span/span")).click();
        PageOperation.WebDriverWaitByXpath(driver, "//div[@id='panel-profile-settings']");
        /*        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(new ExpectedCondition<Boolean>() {
            @Nullable
            @Override
            public Boolean apply(@Nullable WebDriver webDriver) {
                return TestUtil.getCurrentUrl(driver).contains("?code=");
            }
        });*/
    }

	public void clicktoSignupIncomplete() {
		driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath("//div[@class='panel-action-area']/a[1]/span/span")).click();
		//PageOperation.WebDriverWaitByXpath(driver, "//div[@id='panel-twophase-request-confirmation']");

	}
	 public void clicktoSigninComplete() {
		driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath("//div[@class='panel-action-area']/a[1]/span/span")).click();
		//PageOperation.WebDriverWaitByXpath(driver, "//div[@id='panel-twophase-request-confirmation']");

	}

    public void clickToSignup() {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.linkText("Sign up now for free.")).click();
    }

    public String getEmailLabel() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//div[@id='panel-login']/div[1]/div[1]/div[1]/ul/li[1]/div/label")).getText();
    }

    public String getPoweredBy() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//span[@class='poweredby']")).getText();
    }

    public String getCreateAccountButtonString() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//a[@class='registration-link']")).getText();
    }

    public String getPasswordLabel() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//li[@class='password-container']/div/label")).getText();
    }

    public String getPwdLabel() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//div[@id='panel-login']/div[1]/div[1]/div[1]/ul/li[2]/div/label")).getText();
    }

    public String getForgotPasswordLink() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//div[@id='panel-login']/div[1]/div[1]/div[1]/ul/li[2]/a")).getText();
    }

    public String getLoginButtonString() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//a[@id='btnLogin']/span/span")).getText();
    }

    public String getRememberMeLabel() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//label[@id='label-rememberMe']")).getText();
    }

    public String getCopyright() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//form[@id='loginForm']/div[5]/span")).getText();
    }

    public String getLinkOrigin() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//form[@id='loginForm']/div[5]/ul/li[1]/a/span")).getText();
    }

    public String getLinkLegal() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//form[@id='loginForm']/div[5]/ul/li[2]/a/span")).getText();
    }

    public String getLinkService() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//form[@id='loginForm']/div[5]/ul/li[3]/a/span")).getText();
    }

    public String getLinkPolicy() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//form[@id='loginForm']/div[5]/ul/li[4]/a/span")).getText();
    }

    public String getLinkCorporate() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//form[@id='loginForm']/div[5]/ul/li[5]/a/span")).getText();
    }

    public String getLinkTerms() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//form[@id='loginForm']/div[5]/ul/li[6]/a/span")).getText();
    }

    public String getLinkContactUs() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//form[@id='loginForm']/div[5]/ul/li[7]/a/span")).getText();
    }

    public String getLinkRegister() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//div[@id='panel-registration-box']/div/p[2]/a")).getText();
    }

    public void clickSkipEmailVerification() throws Exception {
        /*driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@id='panel-profile-confirmation']/div[2]/a[2]/span/span")).click();
        WebDriverWait wait = new WebDriverWait(driver, 30, 1000);
        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return TestUtil.getCurrentUrl(driver).contains(redirectUri) || TestUtil.getCurrentUrl(driver).contains("lockbox.ea.com");
            }
        });*/
    }

    public void setCaptcha(String value) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(value);
        PageOperation.SendKeyToElementAfterWait(driver, "recaptcha_response_field", value);
    }

    //use when twoFactorAuthenticationEnabled=true and no security.twofactor in client config
    public void responseToChallengeTFACode_Legacy(String twoFactorCode, Boolean checkTrustThisDevice) {
        PageOperation.SendKeyToElement(driver, "twoFactorCode", twoFactorCode);
        By trustThisDevice = By.id("trustThisDevice");
        if((checkTrustThisDevice && (!driver.findElement(trustThisDevice).isSelected()))
                || (!checkTrustThisDevice && (driver.findElement(trustThisDevice).isSelected()))){
            By trustThisDeviceLabel = By.id("label-trustThisDevice");
            driver.findElement(trustThisDeviceLabel).click();
        }
        By btn_submit = By.id("btnTFALogin");
        driver.findElement(btn_submit).click();
    }
    //use when security.twofactor is in client config
    public void responseToChallengeTFACode_New(String twoFactorCode, Boolean checkTrustThisDevice) {
        PageOperation.SendKeyToElement(driver, "twofactorCode", twoFactorCode);
        By trustThisDevice = By.id("trustThisDevice");
        if((checkTrustThisDevice && (!driver.findElement(trustThisDevice).isSelected()))
                || (!checkTrustThisDevice && (driver.findElement(trustThisDevice).isSelected()))){
            By trustThisDeviceLabel = By.id("label-trustThisDevice");
            driver.findElement(trustThisDeviceLabel).click();
        }
        By btn_submit = By.id("btnTFAVerify");
        driver.findElement(btn_submit).click();
    }
    public void responseToChallengeTFACode_WithoutCheckTrustBox(String twoFactorCode) {
        PageOperation.SendKeyToElement(driver, "twofactorCode", twoFactorCode);
        By btn_submit = By.id("btnTFAVerify");
        driver.findElement(btn_submit).click();
    }
    public void skipUpdateToAppAuth() {
        driver.findElement(By.id(cancelButton)).click();
    }

    public String getFooter(int index) {
        return getElementStringByXPath("//form[@id='loginForm']/div[5]/ul/li[" + index + "]/a/span");
    }

	public String getFooter1(int index) {
		return getElementStringByXPath(".//*[@id='loginForm']/div[5]/ul/li[" + index + "]/a/span");
	}

	public String getFooter2(int index) {
		return getElementStringByXPath("//form[@id='resetpasswordformrequest']/div[2]/ul/li[" + index + "]/a/span");
	}

	public String getCopyright1() {
		return getElementStringByXPath(".//*[@id='loginForm']/div[5]/span");
	}

	public String getCopyright2() {
		return getElementStringByXPath(".//*[@id='resetpasswordformrequest']/div[2]/span");
	}

    public String getResetPwdHeader() {
        return getElementStringByXPath("//div[@id='panel-reset-password-request']/div[1]/div/div/h1");
    }

    public String getResetPwdRequestCopy() {
        return getElementStringByXPath("//div[@id='panel-reset-password-request']/div[1]/div/div/p");
    }

    public String getResetPwdEmailLabel() {
        return getElementStringByXPath("//li[@id='origin-reset-password-request-email-container']/div/label");
    }

    public String getResetPwdCaptLabel() {
        return getElementStringByXPath("//li[@id='origin-reset-password-request-captcha-container']/div/label");
    }

    public String getSubmitBtnString() {
        return getElementStringByXPath("//div[@id='panel-reset-password-request']/div[2]/a/span/span");
    }

    public String getResetPwdConfirmationHeader() {
        return getElementStringByXPath("//div[@id='panel-reset-password-request-confirmation']/div[1]/div/h1");
    }

    public String getResetPwdConfirmationCopy1() {
        return getElementStringByXPath("//div[@id='panel-reset-password-request-confirmation']/div[1]/div/p[1]");
    }

    public String getResetPwdConfirmationCopy2() {
        return getElementStringByXPath("//div[@id='panel-reset-password-request-confirmation']/div[1]/div/p[2]");
    }

    public String getResetPwdConfirmationCopy3() {
        return getElementStringByXPath("//div[@id='panel-reset-password-request-confirmation']/div[1]/div/p[3]");
    }

    public String getResetPwdConfirmationCopy() {
        return getResetPwdConfirmationCopy1() + getResetPwdConfirmationCopy2() + getResetPwdConfirmationCopy3();
    }

    public String getResetPwdDescription() {
        return getElementStringByXPath("//div[@id='panel-reset-password']/div[1]/div/h1");
    }

    public String getResetPwdCopy() {
        return getElementStringByXPath("//div[@id='panel-reset-password']/div[1]/div/p");
    }

    public String getResetPwdSuccessHeader() {
        return getElementStringByXPath("//div[@id='panel-reset-password-success']/div[1]/div/h1");
    }

    public String getResetPwdSuccessCopy() {
        return getElementStringByXPath("//div[@id='panel-reset-password-success']/div[1]/div/p");
    }

    public String getUpgradeCopy() {
        return getElementStringByXPath("//div[@id='panel-profile-upgrade']/div/div/p[1]");
    }

    public boolean doesWebElementExist( By selector)
    {
        driver.switchTo().window(this.operationWindowHandle);
        try {
            driver.findElement(selector);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }




	public String getContent(String path) {
		return getElementStringByXPath(path);
	}

	public void clicktoCreateAccountOnSigninPage() {
		driver.switchTo().window(this.operationWindowHandle);
		driver.findElement(By.xpath(".//*[@id='panel-registration-box']/div/a/span/span")).click();
	}

    public String getTwoPhaseFooter(int index ) {
        return getElementStringByXPath(".//*[@id='twophaseformrequest']/div[2]/ul/li["+index+"]/a/span");
    }

    public String getCompleteYourAccountCopyRight() {
        return getElementStringByXPath(".//*[@id='twophaseformrequest']/div[2]/span") ;
    }

    public void clickToSignupMobileGame(){
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//*[@id=\"signupBtn-Short\"]/a/span")).click();
    }
}
